function error(text){
    return '<p style="text-align:center;"><span class="ui-icon ui-icon-alert" style="text-align:center;float: left; margin-right: .3em;"></span>'+
    text+'</p>'
}

function info(text){
    return '<p style="text-align:center;"><span class="ui-icon ui-icon-info" style="text-align:center;float: left; margin-right: .3em;"></span>'+
    text+'</p>'
}

function isInteger(value){
    var intRegex = /^\d+$/;
    if(intRegex.test(value)){
        return true;
    }
    return false;
}

function startsWith(str, prefix) {
    return str.indexOf(prefix) === 0;
}

function sendSelectedTheme(){
    document.themeForm.submit();
}

function updateClock ()
{
    var currentTime = new Date();
  
    var currentDay = currentTime.getDate();
    currentDay = ( currentDay == 0 ) ? "Sunday" : currentDay;
    currentDay = ( currentDay == 1 ) ? "Monday" : currentDay;
    currentDay = ( currentDay == 2 ) ? "Tuesday" : currentDay;
    currentDay = ( currentDay == 3 ) ? "Wednesday" : currentDay;
    currentDay = ( currentDay == 4 ) ? "Thursday" : currentDay;
    currentDay = ( currentDay == 5 ) ? "Friday" : currentDay;
    currentDay = ( currentDay == 6 ) ? "Saturday" : currentDay;

    var currentMonth = currentTime.getMonth();
    currentMonth = ( currentMonth == 0 ) ? "January" : currentMonth;
    currentMonth = ( currentMonth == 1 ) ? "February" : currentMonth;
    currentMonth = ( currentMonth == 2 ) ? "March" : currentMonth;
    currentMonth = ( currentMonth == 3 ) ? "April" : currentMonth;
    currentMonth = ( currentMonth == 4 ) ? "May" : currentMonth;
    currentMonth = ( currentMonth == 5 ) ? "June" : currentMonth;
    currentMonth = ( currentMonth == 6 ) ? "July" : currentMonth;
    currentMonth = ( currentMonth == 7 ) ? "August" : currentMonth;
    currentMonth = ( currentMonth == 8 ) ? "September" : currentMonth;
    currentMonth = ( currentMonth == 9 ) ? "October" : currentMonth;
    currentMonth = ( currentMonth == 10) ? "November" : currentMonth;
    currentMonth = ( currentMonth == 11) ? "December" : currentMonth;

    var currentDate = currentTime.getDate();
    currentDate = ( currentDate == 1 || currentDate == 21 || currentDate == 31 ) ? currentDate + "st" : currentDate;
    currentDate = ( currentDate == 2 || currentDate == 22 ) ? currentDate + "nd" : currentDate;
    currentDate = ( currentDate == 3 ) || currentDate == 23 ? currentDate + "rd" : currentDate;
    currentDate = ( currentDate > 3 || currentDate < 21 || currentDate > 23 || currentDate < 31 ) ? currentDate + "th" : currentDate;

    var currentHours = currentTime.getHours();
    var currentMinutes = currentTime.getMinutes();
    var currentSeconds = currentTime.getSeconds();
    currentSeconds = (currentSeconds < 10 ? "0" : "") +currentSeconds;
    currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
    var timeOfDay = ( currentHours < 12 ) ? "AM" : "PM";
    currentHours = ( currentHours > 12 ) ? currentHours - 12 : currentHours;
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;
    var currentTimeString = "Friday" + ", " + currentMonth +  " " + currentDate + ", 2011";
    var currentTime1 = currentHours + ":" + currentMinutes + ":"+ currentSeconds +" "+ timeOfDay;
    $('span#date').html(currentTimeString);
    $('span#time').html(currentTime1);
}

$(document).ready(function(){
    updateClock();
    setInterval('updateClock()',1000);
    
    $('tr[errorFor] > td').addClass("ui-state-error ui-corner-all");
    $('.errorMessage').before('<span class="ui-icon ui-icon-alert"></span>');

    $('div#mainMenu input').click(function(e){
        var element = $(this).attr("id");
        if(element=='mainMenuHome'){
            window.location = "/Cashmate/home.jsp"
        }else if(element=='mainMenuLoan'){
            $('div#leftContainer > div').slideUp('fast',function(){
                $('div#loanSubMenuContainer').slideDown('fast');
            });
        }
    });

    $('a.subMenu').click(function(e){
        var element = this.id;
        $('a.subMenu').removeClass("ui-state-highlight");
        $(this).addClass("ui-state-highlight");
        $('div.innerSubMenu').stop(true,true).slideUp('fast',function(){
            if(element == 'subMenuLoanApplication'){
                $('div#loanApplicationSubMenu').slideDown('fast');
            }else if(element == 'subMenuLoanApplicationHo'){
                $('div#loanApplicationHoSubMenu').slideDown('fast');
            }else if(element == 'subMenuLoanRequest'){
                $('div#loanRequestSubMenu').slideDown('fast');
            }else if(element == 'subMenuLoanRelease'){
                $('div#loanReleaseSubMenu').slideDown('fast');
            }else if(element== 'subMenuLoanCollection'){
                $('div#loanCollectionSubMenu').slideDown('fast');
            }else if(element== 'subMenuLoanApproval'){
                $('div#loanApprovalSubMenu').slideDown('fast');
            }else if(element== 'subMenuLoanCheckRegister'){
                $('div#loanCheckRegisterSubMenu').slideDown('fast');
            }else if(element == 'subMenuLoanVerification'){
                window.location.href="/Cashmate/loanverification.jsp";
            }else if(element == 'subMenuCCR'){
                window.location.href="/Cashmate/ccr.jsp";
            }
        });
    });

    $('a.innerSubMenu').click(function(e){
        var element = this.id.split("_");
        $('a.innerSubMenu').removeClass("ui-state-highlight");
        $(this).addClass("ui-state-highlight");
        if(startsWith(element[0],"loanApplication")){
            window.location="loanapplication.jsp?cat="+element[1];
        }else if(startsWith(element[0],"loanApplicationHo")){
            window.location="loanapplication.jsp?cat="+element[1];
        }else if(startsWith(element[0],"loanCollection")){
            window.location="/Cashmate/loancollection.jsp?cat="+element[1];
        }else if(startsWith(element[0],"loanRequest")){
            window.location="/Cashmate/loanrequest.jsp?cat="+element[1];
        }else if(startsWith(element[0],"loanApproval")){
            window.location="/Cashmate/loanapproval.jsp?cat="+element[1];
        }else if(startsWith(element[0],"loanRelease")){
            window.location="/Cashmate/loanrelease.jsp?cat="+element[1];
        }else if(startsWith(element[0],"loanCheckRegister")){
            window.location="/Cashmate/loancheckregister.jsp?cat="+element[1];
        }
    });

    $('.jqButton').hover(function(){
        $(this).addClass("ui-state-hover");
    },function(){
        $(this).removeClass("ui-state-hover");
    });
    
    $("#mainMenuAdministration").attr("disabled", "disabled");
});

